package com.caipiao.crawler.pailie;

import com.alibaba.fastjson.JSON;
import com.caipiao.crawler.mapper.LotteryCurrentMapper;
import com.caipiao.crawler.mapper.LotteryNumberMapper;
import com.caipiao.crawler.mapper.LotteryTicketMapper;
import com.caipiao.crawler.model.*;
import com.caipiao.crawler.zhongjiangRule.ZhongJiangRule;
import com.caipiao.util.DBTools;
import org.apache.ibatis.session.SqlSession;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by adminitrator on 2018/7/28.
 */
public class PailieResultPipeline implements Pipeline {

    @Override
    public synchronized void process(ResultItems resultItems, Task task) {
        LotteryNumber m3 = resultItems.get("model3");
        LotteryNumber m5 = resultItems.get("model5");
        System.err.println("pipe process:" + Thread.currentThread().getId());
        if(m3 == null|| m5 ==null) {
            return;
        }
        commit(m3,m5);
    }

    public void commit(LotteryNumber model3, LotteryNumber model5) {
        System.err.println("commit");
        System.err.println(JSON.toJSONString(model3));
        System.err.println(JSON.toJSONString(model5));
        //TODO 写数据库，需要判断不存在的情况才写入
        SqlSession session = DBTools.getSession();
        LotteryNumberMapper mapper = session.getMapper(LotteryNumberMapper.class);
        LotteryTicketMapper ticketMapper = session.getMapper(LotteryTicketMapper.class);
        LotteryCurrentMapper currentMapper = session.getMapper(LotteryCurrentMapper.class);
        Map map = new HashMap();
        map.put("term",model3.getTerm());
        map.put("type",model3.getType());
        int count = mapper.selectCountByTerm(map);
        if(count==0) {
            mapper.insertModel(model3);
            map.clear();
            map.put("term",model3.getTerm());
            map.put("name","pailie3");
            map.put("updated_time",(int) (System.currentTimeMillis()/1000L));
            currentMapper.updateCurrent(map);
            mapper.insertModel(model5);
            map.clear();
            map.put("term",model5.getTerm());
            map.put("name","pailie5");
            map.put("updated_time",(int) (System.currentTimeMillis()/1000L));
            currentMapper.updateCurrent(map);
        }
        session.commit();
        //TODO 如果第一次写入，触发更新判断是否中奖的逻辑
        if(count==0){
            String prizeNumber = model3.getNumber(); //中奖号码
            Map mapPl3 = new HashMap<String,Object>();
            mapPl3.put("term",model3.getTerm());
            mapPl3.put("type","3");
            map.put("status","1");
            Map mapPl5 = new HashMap<String,Object>();
            mapPl5.put("term",model5.getTerm());
            mapPl5.put("type","4");
            map.put("status","1");
            List<LotteryTicket> ticketListPl3 = ticketMapper.selectByTermAndType(mapPl3);
            List<LotteryTicket> ticketListPl5 = ticketMapper.selectByTermAndType(mapPl5);
            for(LotteryTicket lotteryTicket : ticketListPl3){
                String number = lotteryTicket.getNumber();
                Map ticketMapForPl3 = ZhongJiangRule.pailie3Rule(number,model3.getNumber(),model3,lotteryTicket.getMultiple());
                ticketMapForPl3.put("id",lotteryTicket.getId());
                ticketMapForPl3.put("updated_time",(int) (System.currentTimeMillis()/1000L));
                ticketMapper.updateTicketById(ticketMapForPl3);
            }
            for(LotteryTicket lotteryTicket : ticketListPl5){
                String number = lotteryTicket.getNumber();
                Map ticketMapForPl5 = ZhongJiangRule.pailie5Rule(number,model5.getNumber(),model5,lotteryTicket.getMultiple());
                ticketMapForPl5.put("id",lotteryTicket.getId());
                ticketMapForPl5.put("updated_time",(int) (System.currentTimeMillis()/1000L));
                ticketMapper.updateTicketById(ticketMapForPl5);
            }
            session.commit();
            session.close();
        }
    }

}
